/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xmlasm.action;

import com.opensymphony.xwork2.ActionSupport;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
import org.xml.sax.SAXException;
import utils.XMLUtils;
import xmlasm.sync.FilePathHolder;

/**
 *
 * @author Hoang
 */
public class OtherAction extends ActionSupport {

    private String documentName;
    private String outputFO;

    public String getOutputFO() {
        return outputFO;
    }

    public void setOutputFO(String outputFO) {
        this.outputFO = outputFO;
    }

    public String getDocumentName() {
        return documentName;
    }

    public void setDocumentName(String documentName) {
        this.documentName = documentName;
    }
    private InputStream inputStream;

    public InputStream getInputStream() {
        return inputStream;
    }

    public String getXML() {
        try {
            File f = new File(FilePathHolder.getInstance().getWebPath() + documentName);
            inputStream = new FileInputStream(f);
            return SUCCESS;
        } catch (FileNotFoundException ex) {
            Logger.getLogger(OtherAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ERROR;
    }

    public String getPDF() throws TransformerConfigurationException {
        try {

            //Tạo sẵn stream để chứa file pdf sẽ tạo
            String webPath = FilePathHolder.getInstance().getWebPath();
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

            //Khoi tao factory
            FopFactory ff = FopFactory.newInstance();
            File config = new File(webPath + "WEB-INF/pdf/config/fop.xconf");
            ff.setUserConfig(config);
            Fop fop = ff.newFop(MimeConstants.MIME_PDF, outputStream);

            //Bat dau transform ra file PDF
            TransformerFactory tff = TransformerFactory.newInstance();
            Transformer tf = tff.newTransformer();

            //Source la filename được chain từ biến outputFO vua roi
            Source src = new StreamSource(new File(outputFO));
            Result rsl = new SAXResult(fop.getDefaultHandler());
            tf.transform(src, rsl);

            //Bo vao stream de dua ra
            inputStream = new ByteArrayInputStream(outputStream.toByteArray());
            return SUCCESS;
        } catch (IOException ex) {
            Logger.getLogger(OtherAction.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(OtherAction.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(OtherAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ERROR;
    }
}
